<?php

require_once( '../system/smarty.inc' );

require_once( '../system/mdao/Class_M_MAP_GROUP_WEEK.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );

require_once( '../system/Class_ERROR.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/Class_PWD.php' );

Class_PWD::valid_auth(AOKI_AUTH_MAP_GROUP,$login['staff_auth']);

$isG7  = FALSE;
$isG23  = FALSE;
if   ($login['staff_auth'] == 'G-7') {
	$isG7 = TRUE;
}
$smarty->assign('isG7', $isG7 );

$dosearch = FALSE ;

$dao        = new Class_M_MAP_GROUP_WEEK;
$err        = new Class_ERROR;
$matrix_chk = $dao->get_matrix_chk_fields();

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ||
	 isset( $_GET['hist'] ) &&  (  $_GET['hist'] == "2" ) ) {
	$forms =  $_SESSION['MSEARCH']['MAP_GROUP_WEEK'];
}else {
	$forms = $_GET;
	$_SESSION['MSEARCH']['MAP_GROUP_WEEK'] = $forms;
}

$wherearr =  array();

if ( isset( $forms['week_cd'] ) &&  ( !empty( $forms['week_cd'] ) ) ){
	$wherearr[] = sprintf(" b.matrix_cd =  '%s' ",  $forms['week_cd'] );
}

if ( isset( $forms['sort'] ) &&  ( !empty( $forms['sort'] ) ) ){
	$wherearr[] = sprintf(" a.sort =  '%s' ",  $forms['sort'] );
}

if ( isset( $forms['map_no_s'] ) &&  ( !empty( $forms['map_no_s'] ) ) ){
	$wherearr[] = sprintf(" a.map_no =  '%s' ",  $forms['map_no_s'] );
}


if ( isset( $forms['upd_f'] ) &&  ( !empty( $forms['upd_f'] ) ) ){
	$wherearr[] = Class_PWD::get_u_time_where_sql($forms['upd_f'], 1, "b" );
}

if ( isset( $forms['upd_t'] ) &&  ( !empty( $forms['upd_t'] ) ) ){
	$wherearr[] = Class_PWD::get_u_time_where_sql($forms['upd_t'],2, "b" );
}

//---CSVダウンロード　処理開始
if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ){
	/*
	$_GET["csv"] = "";
	$tmpfilename = "ins_shop".date("Ymd_His");
	header("Content-Type: application/octet-stream"); 
	header("Content-Disposition: attachment; filename=$tmpfilename.csv");
	$data = $dao->search( $wherearr );
	$colums = array();
	//列名
	$colums["name"] = array('shop_cd','shop_name','block_name','local_name','postcode','address','tel','center','u_time','m_user_name');
	$colums["type"] = array();
	foreach ($colums as $k => $v) {
		$colums["type"][$v] = 1;
	}
	
	$dao->output_csv($data, $colums);
	*/
	exit;
}
//---CSVダウンロード　処理完了

//-------------------------------コピー処理開始-------------------------------
if ( isset( $forms['mapoperflg'] ) && (  $forms['mapoperflg']  == "copy" ) ){
	//$map_no = $forms['map_no'];
	//$matrix_cd = $dao->getMaxWeek ( $map_no );
	
	$from_matrix_cd =  $forms['week_cd'];
	$to_matrix_cd   =  $forms['week_cdto'];
	
	
	if ($to_matrix_cd) {
		//
		//if ( $dao->exits($where) ) {
		//	$sqlmessage = "該当週のデータは既に存在するため、コピーできません。";
		//}else {
			try {
				$page=NULL;
				$data = $dao->search( $wherearr, $page );
				
				$dao->begin_trans();
				$executeflg = TRUE;
				$values   = array();
				
				
				$values["matrix_cd"] =  $to_matrix_cd;
				foreach ($data as $k=>$v){
					$where = sprintf(" map_no = '%s' AND matrix_cd  = '%s'", $v['map_no'], $values["matrix_cd"] );
					if ( !$dao->exits($where) ) {
						$values["map_no"]    =  $v['map_no'];
						if ($dao->add($values)) {
						} else {
							$executeflg = FALSE;
						}
					}
					
				}
				/*
				if (!$dao->copy_shops($map_no, $matrix_cd, $forms['week_cd'])) {
					$executeflg = FALSE;
				}
				*/
				if (!$dao->copy_to_week($wherearr,$from_matrix_cd, $to_matrix_cd)) {
					$executeflg = FALSE;
				}
				
				if  ($executeflg) {
					$dao->commit_trans();
					//$smarty->assign('message', '保存が完了しました。');
					$sqlmessage = "コピーが完了しました。";
				} else {
					$dao->rollback_trans();
					//$smarty->assign('message', '保存が失敗しました。');
					$sqlmessage = "コピー保存が失敗しました。";
				}
			} catch (Exception $e) {
				$dao->rollback_trans();
				$sqlmessage = "ＤＢエラーで失敗しました。";
			}
		}
	//} else {
	//	$sqlmessage = "該当マップNOは存在しません。";
	//}
}
//-------------------------------コピー処理完了-------------------------------
//-------------------------------削除処理開始-------------------------------
if ( isset( $forms['mapoperflg'] ) && (  $forms['mapoperflg']  == "nodel" ) ){
	$map_no = $forms['map_no_del'];
	if ($dao->del_map_no($map_no, $forms['week_cd'])) {
		$sqldelmsg = "マップNO{$aoki_map_sort[$map_no]}の削除が成功しました。";
	} else {
		$sqldelmsg = "マップNO{$aoki_map_sort[$map_no]}の削除が失敗しました。";
	}
	
}

if ( isset( $forms['mapoperflg'] ) && (  $forms['mapoperflg']  == "sortdel" ) ){
	$sort_no = $forms['sort_del'];
	if ($dao->del_sort($sort_no, $forms['week_cd'])) {
		$sqldelmsg = "{$aoki_map_sort[$sort_no]}の削除が成功しました。";
	} else {
		$sqldelmsg = "{$aoki_map_sort[$sort_no]}の削除が失敗しました。";
	}
}
//-------------------------------削除処理完了-------------------------------

if ( isset( $forms['search'] ) && (  !empty( $forms['search'] ) ) ){
	$dosearch = TRUE;
}

if( isset( $forms['page'] ) && is_numeric( $forms['page'] ) ){
	$dosearch = TRUE;
	$page['current'] = $forms['page'];	
}else{
	$page['current'] = 1;
}
$page['item'] = PAGE_MAX;
if ($dosearch) {
	//$page内容は関数内部更新されます。
	foreach ($matrix_chk as $kcol => $chkval) {
		$err_mes[$kcol]  = $err->check( $forms[$kcol], $chkval );
	}
	if ( $err->clear ){
		$data = $dao->search( $wherearr, $page );
		foreach ( $data as $k => $v ) {
			$data[$k]["shop_num"] = $dao->getShopCount($v["map_no"], $v["matrix_cd"]);
		}
	}
}

if (empty($forms["year4"])) {
	$forms["year4"] = date('Y');
}

if (empty($forms["year4to"])) {
	$forms["year4to"] = $forms["year4"] ;
}
if( isset($forms) ) $smarty->assign('forms', $forms);
if( isset($data) )  $smarty->assign('data', $data);
if( isset($page) )  $smarty->assign('page', $page);
if( isset($err_mes ) ) $smarty->assign('err', $err_mes);
if( isset($sqlmessage ) ) $smarty->assign('errcopy', $sqlmessage);
if( isset($sqldelmsg ) ) $smarty->assign('errdel', $sqldelmsg);

$smarty->assign('year4',  Class_PWD::get_years());

$week= Class_M_OTHER::get_week_array( $forms["year4"], PLEASE_SELECT_ARR );
$smarty->assign('week', $week );

$weekto  = Class_M_OTHER::get_week_array( $forms["year4to"], PLEASE_SELECT_ARR );
$smarty->assign('weekto', $weekto );

//$smarty->assign('map_sort', $aoki_map_sort );
$map_sort[""] = "すべて";
foreach ($aoki_map_sort as $k => $v) {
	$map_sort[$k] = $v;
}
//$smarty->assign('map_sort', $aoki_map_sort );

$smarty->assign('map_sort', $map_sort );
$map_sort[""] = "選択してください";
$smarty->assign('map_sort_del', $map_sort );


$smarty->assign('menu',  $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );
$smarty->assign('pageing', $smarty->fetch('page.html') );
$smarty->display('mmapshop_index.html');

?>